package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.markingsystem.FragmentReadPaper;
import com.everflourish.yeah100.act.markingsystem.ObjectAnswerActivity;
import com.everflourish.yeah100.act.markingsystem.SubjectAnswerActivity;
import com.everflourish.yeah100.entity.marking.Student;
import com.everflourish.yeah100.entity.marking.StudentModel;
import com.everflourish.yeah100.ui.UnderlineTextView;
import com.everflourish.yeah100.utils.AnswerUtil;
import com.everflourish.yeah100.utils.CommonUtil;
import com.everflourish.yeah100.utils.IntentUtil;
import com.everflourish.yeah100.utils.TopicUtil;
import com.everflourish.yeah100.utils.constant.StudentStatus;

import java.util.List;
import java.util.Map;

/**
 * 作者：Created by liqisi on 2016/6/24.
 * 时间：2016/6/24
 */
public class FragmentStudentAdapter extends RecyclerView.Adapter<FragmentStudentAdapter.StudentAnswerViewHolder> {

    public static final int UPLOADING = 1; // 上传中
    public static final int UPLOAD_FAILURE = 2; // 上传失败
    public static final int COMPLETE = 3; // 完成
    public static final int WAIT = 4; // 等待s

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<StudentModel> mList;
    private FragmentReadPaper mFragment;

    public FragmentStudentAdapter(Context mContext, List<StudentModel> mList, FragmentReadPaper mFragment) {
        this.mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mList = mList;
        this.mFragment = mFragment;
    }

    @Override
    public StudentAnswerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_student_answer, parent, false);
        StudentAnswerViewHolder viewHolder = new StudentAnswerViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StudentAnswerViewHolder holder, int position) {
        Student student = mList.get(position).getStudent();
        holder.mStudentName.setText(student.getName());
        holder.mStudentNo.setText(student.getNumber());
        if (student.getAnswer() != null) {
            if (student.getStatus().equals(StudentStatus.C.text)) {
                holder.mStudentSubScore.setText("0");
            } else {
                holder.mStudentSubScore.setText(CommonUtil.subZeroAndDot(student.getAnswer()
                        .getSubjectScores()));
            }
        } else {
            holder.mStudentSubScore.setText("--");
        }
        if (student.getAnswer() != null) {
            float studentGrade = 0f;
            List<Map<String, Object>> answerList = AnswerUtil.answerToStudentList(
                    student.getAnswer().getObjectAnswers(), mFragment.mTopics);
            studentGrade = AnswerUtil.getStudentGrade(mFragment.mTopics,
                    mFragment.mStaList, answerList);
            holder.mStudentObjScore.setText(CommonUtil.subZeroAndDot(studentGrade));
            if (!TopicUtil
                    .isHaveSubTopic(mFragment.mTopics)) {
                holder.mStudentSubScore.setVisibility(View.GONE);
                holder.mStudentTotalScore.setText("" + studentGrade);
            } else {
                holder.mStudentSubScore.setVisibility(View.VISIBLE);
                holder.mStudentTotalScore.setText(CommonUtil.subZeroAndDot(student.getAnswer()
                        .getSubjectScores() + studentGrade));
            }
            if (student.getStatus().equals(StudentStatus.C.text)) {
                holder.mStudentObjScore.setText("0");
                holder.mStudentTotalScore.setText("0");
            }
        }
        if ((position + 1) % 2 == 0) {  // 双
            holder.mStudentItemLl.setBackgroundColor(mContext
                    .getResources()
                    .getColor(R.color.e_student_item_single));
        } else { // 单
            holder.mStudentItemLl.setBackgroundColor(mContext
                    .getResources()
                    .getColor(R.color.e_student_item_double));
        }
        holder.mStudentObjScore.setOnClickListener(new MyOnclickListener(holder, position, mFragment));
        holder.mStudentSubScore.setOnClickListener(new MyOnclickListener(holder, position, mFragment));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class StudentAnswerViewHolder extends RecyclerView.ViewHolder {

        private TextView mStudentName;
        private TextView mStudentNo;
        private UnderlineTextView mStudentObjScore;
        private UnderlineTextView mStudentSubScore;
        private TextView mStudentTotalScore;
        private LinearLayout mStudentItemLl;

        public StudentAnswerViewHolder(View itemView) {
            super(itemView);
            mStudentName = (TextView) itemView.findViewById(R.id.item_e_student_name);
            mStudentNo = (TextView) itemView.findViewById(R.id.item_e_student_no);
            mStudentObjScore = (UnderlineTextView) itemView.findViewById(R.id.item_e_student_obj_score);
            mStudentSubScore = (UnderlineTextView) itemView.findViewById(R.id.item_e_student_sub_score);
            mStudentTotalScore = (TextView) itemView.findViewById(R.id.item_e_student_total_score);
            mStudentItemLl = (LinearLayout) itemView.findViewById(R.id.item_e_student_ll);
        }
    }

    class MyOnclickListener implements View.OnClickListener {

        private StudentAnswerViewHolder viewHolder;
        private int position;
        private FragmentReadPaper fragment;

        public MyOnclickListener(StudentAnswerViewHolder viewHolder, int position, FragmentReadPaper fragment) {
            this.viewHolder = viewHolder;
            this.position = position;
            this.fragment = fragment;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_e_student_obj_score:
                    Intent intent = new Intent(mFragment.mMarkingMenuActivity,
                            ObjectAnswerActivity.class);
                    intent.putExtra(IntentUtil.STUDENT_ANSWER, mList.get(position).getStudent());
                    intent.putExtra(IntentUtil.TOPIC, mFragment.mTopics);
                    intent.putExtra(IntentUtil.STANDARD_ANSWER, mFragment.mStaList);
                    mFragment.mMarkingMenuActivity.startActivityForResult(intent, 0x210);
                    break;
                case R.id.item_e_student_sub_score:
                    Intent subjectIntent = new Intent(mFragment.mMarkingMenuActivity,
                            SubjectAnswerActivity.class);
                    subjectIntent.putExtra(IntentUtil.STUDENT_ANSWER, mList.get(position).getStudent());
                    subjectIntent.putExtra(IntentUtil.TOPIC, mFragment.mTopics);
//                    subjectIntent.putExtra(IntentUtil.STANDARD_ANSWER, mFragment.mStaList);
                    mFragment.mMarkingMenuActivity.startActivityForResult(subjectIntent, 0x220);
                    break;
            }
        }
    }
}